/*
 * @Description: 存储
 * @Author: yech
 * @Date: 2021-09-07
 */
import type { StoreData } from '@/types';

const KEY = 'glucose_monitor_key';
const DEFAULT_MONITOR_VALUE = '{"user":"匿名用户","glucose":[],"weight":[]}';

export function readData(): StoreData {
    const data = JSON.parse(localStorage.getItem(KEY) || DEFAULT_MONITOR_VALUE);
    if (data['glucose'] === undefined) {
        data['glucose'] = [];
    }
    if (data['weight'] === undefined) {
        data['weight'] = [];
    }
    return data;
}

export function saveData(monitorData: StoreData): void {
    localStorage.setItem(KEY, JSON.stringify(monitorData));
}
